package cn.edu.nju.ws.sview.servlets;

import java.io.IOException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import cn.edu.nju.ws.sview.views.Comment;

/**
 * Servlet implementation class CommentServlet
 */
@WebServlet("/CommentServlet")
public class CommentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String option=null;
		String json=null;
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		if(request.getParameter("getDescription")!=null){
			json = request.getParameter("getDescription");
			option="getDescription";
		}
		else if(request.getParameter("addComment")!=null){
			json = request.getParameter("addComment");
			option="addComment";
		}
		
		if(option.equals("getDescription")){
			String str = URLDecoder.decode(json, "utf-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			int etype = Integer.parseInt((String) regtJSON.get("etype"));
			int eid = Integer.parseInt((String) regtJSON.get("eid"));
			String lang = (String) regtJSON.get("lang");
			String groupName = (String) regtJSON.get("groupName");

			Comment comment = null;
			comment = Comment.getCommentByMemberLangGroup(etype, eid, lang,
					groupName);
			JSONObject obj = new JSONObject();
			obj.put("id", comment.getID());
			obj.put("groupName", comment.getGroupName());
			long time = comment.getTime();
			Date date = new Date(time);
			SimpleDateFormat format = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			String timeStr = format.format(date);
			obj.put("time", timeStr);
			obj.put("comment", comment.getText());
			response.getWriter().print(obj);
 		}
		else if(option.equals("addComment")){
			String str = URLDecoder.decode(json,"utf-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			int etype = Integer.parseInt((String)regtJSON.get("etype")) ;
			int eid = Integer.parseInt((String)regtJSON.get("eid")) ;
			String groupName= (String)regtJSON.get("groupName") ;
			String text = (String)regtJSON.get("comment") ;
			String lang = (String)regtJSON.get("lang") ;
			
			
			Comment comment = new Comment(groupName,System.currentTimeMillis(),lang,text,etype,eid);
			comment.serialize();
		}
	}

}
